/////////////////////////////////////////////////////////////////////////////
// Name:        imagevalidator.cpp
// Purpose:     wxImageValidator class
// Author:      Julian Smart
// Modified by:
// Created:     July 27th 2007
// RCS-ID:      $Id: imagevalidator.cpp,v 1.2 2007/11/19 07:07:06 anthemion Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#if wxUSE_VALIDATORS

#include "imageutils/imagevalidator.h"

IMPLEMENT_CLASS(wxImageValidator, wxValidator)

BEGIN_EVENT_TABLE(wxImageValidator, wxValidator)
    EVT_LEFT_DOWN(wxImageValidator::OnLeftClick)
END_EVENT_TABLE()

/* XPM */
static char *noschemaicon16x16_xpm[] = {
/* columns rows colors chars-per-pixel */
"16 16 2 1",
"  c Black",
". c #FFFFFF",
/* pixels */
"                ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
" .............. ",
"                "
};

wxImageValidator::wxImageValidator(wxImageBlock* imageBlock): wxValidator()
{
    Init();

    m_imageBlock = imageBlock;
}

wxImageValidator::wxImageValidator(const wxImageValidator& val)
{
    Init();

    Copy(val);
}

bool wxImageValidator::Copy(const wxImageValidator& val)
{
    wxValidator::Copy(val);

    m_imageBlock = val.m_imageBlock;

    return true;
}

// Called to transfer data to the window
bool wxImageValidator::TransferToWindow(void)
{
    wxStaticBitmap* win = wxDynamicCast(GetWindow(), wxStaticBitmap);
    if (win)
    {
        if (m_imageBlock && m_imageBlock->GetData())
        {
            wxImage image;
            if (m_imageBlock->Load(image))
            {
                if (image.Ok())
                {
                    if (image.GetWidth() != 16 || image.GetHeight() != 16)
                        image.Rescale(16, 16, wxIMAGE_QUALITY_HIGH);
                    wxBitmap bitmap(image);
                    if (bitmap.Ok())
                    {
                        win->SetBitmap(bitmap);
                        return true;
                    }
                }
            }
        }
        win->SetBitmap(wxBitmap(noschemaicon16x16_xpm));
    }

    return true;
}

// Called to transfer data from the window
bool wxImageValidator::TransferFromWindow(void)
{
    return true;
}

// Initialize
void wxImageValidator::Init()
{
    m_imageBlock = NULL;
}

// Respond to left click
void wxImageValidator::OnLeftClick(wxMouseEvent& event)
{
    if (!GetWindow())
        return;

    // Get suitable parent
    wxWindow* p = GetWindow()->GetParent();
    while (p && !p->IsTopLevel())
        p = p->GetParent();

    if (m_imageBlock)
    {
        wxString wildCard = wxT("All Files (*.*)|*.*|PNG Files (*.png)|*.png|JPEG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif");
        wxFileDialog dlg(p, _("Image File"), wxEmptyString, wxEmptyString, wildCard, wxFD_OPEN|wxFD_FILE_MUST_EXIST);
        if (dlg.ShowModal() == wxID_OK)
        {
            wxString filename = dlg.GetPath();
            if (wxFileExists(filename))
            {
                int fileType = wxImageBlock::DetermineImageType(filename);
                if (fileType != -1)
                {
                    wxImage image;
                    if (m_imageBlock->MakeImageBlock(filename, fileType, image, false))
                    {
                        TransferToWindow();
                    }
                }
            }
        }
    }
}

#endif
  // wxUSE_VALIDATORS
